package com.hoingk.yamba;

import winterwell.jtwitter.Twitter;
import winterwell.jtwitter.TwitterException;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class StatusActivity extends Activity implements OnClickListener,TextWatcher, OnSharedPreferenceChangeListener {
	private static final String TAG="StatusActivity";
	EditText editText;
	Button updateButton;
	Twitter twitter;
	TextView textCount;
	
	SharedPreferences prefs;
	
	
    /** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.status);
        
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.registerOnSharedPreferenceChangeListener(this);
        
        editText = (EditText) findViewById(R.id.editText);
        updateButton = (Button) findViewById(R.id.updateButton);
        updateButton.setOnClickListener(this);
      
        textCount = (TextView) findViewById(R.id.textCount);
        textCount.setText(Integer.toString(140));
        textCount.setTextColor(Color.GREEN);
        editText.addTextChangedListener(this);
        
        twitter = new Twitter("student","password");        
        twitter.setAPIRootUrl("http://yamba.marakana.com/api");
    }

	private Twitter getTwitter(){
		
			if(twitter == null){
				String username, password, apiRoot;
				username = prefs.getString("username", "");
				password = prefs.getString("password", "");
				apiRoot = prefs.getString("apiRoot", "http://yamba.marakana.com/api");
				
				twitter = new Twitter(username, password);
				twitter.setAPIRootUrl(apiRoot);
			}

		return twitter;
	}
    @Override
	public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
		// TODO Auto-generated method stub
    	twitter = null;
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
			case R.id.itemPrefs:
				startActivity(new Intent(this, PrefsActivity.class));
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	class PostToTwitter extends AsyncTask<String, Integer, String>{
    	
		@Override
		protected String doInBackground(String... statuses) {
			// TODO Auto-generated method stub
			try{
    			winterwell.jtwitter.Status status = twitter.updateStatus(statuses[0]);
    			return status.text;
    		} catch(TwitterException e){
    			Log.e(TAG, e.toString());
    			e.printStackTrace();
    			return "Failed to post";
    		}
		}

		@Override
		protected Object clone() throws CloneNotSupportedException {
			// TODO Auto-generated method stub
			return super.clone();
		}

		@Override
		public boolean equals(Object o) {
			// TODO Auto-generated method stub
			return super.equals(o);
		}

		@Override
		protected void finalize() throws Throwable {
			// TODO Auto-generated method stub
			super.finalize();
		}

		@Override
		public int hashCode() {
			// TODO Auto-generated method stub
			return super.hashCode();
		}

		@Override
		public String toString() {
			// TODO Auto-generated method stub
			return super.toString();
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			Toast.makeText(StatusActivity.this, result, Toast.LENGTH_LONG).show();
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
		}
    }
	@Override
	public void onClick(View v) {
		 //TODO Auto-generated method stub
		try {
			getTwitter().setStatus(editText.getText().toString());
		
		} catch (TwitterException e) {
			Log.d(TAG,"Twitter setStatus failed : " + e);
		}
		
		
	}
	@Override
	public void afterTextChanged(Editable statusText) {
		// TODO Auto-generated method stub
		int count = 140 - statusText.length();
		textCount.setText(Integer.toString(count));
		textCount.setTextColor(Color.GREEN);
		if(count<10)
			textCount.setTextColor(Color.YELLOW);
		if(count<0)
			textCount.setTextColor(Color.RED);
		
	}
	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		
	}
    
}